#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#define PATH "/home/study/lesson8"
#define ID 0x6666
#define SIZE 4096

int main()
{
    key_t key = ftok(PATH, ID);//获取和服务端一样的key值
    if(key < 0){
        perror("ftok!\n");
        return 1;
    }

    int shm = shmget(key, SIZE, IPC_CREAT);//获取服务端创建的共享内存的用户层ID
    if(shm < 0){
        perror("shmget!\n");
        return 2;
    }

    printf("key: %d\n", key);
    printf("shm: %d\n", shm);

    char* mem = (char*)shmat(shm, NULL, 0);//关联共享内存
    char buff[128];
    while(1){
        ssize_t s = read(0, buff, sizeof(buff) - 1);
        buff[s-1] = 0;
        memcpy(mem, buff, sizeof(buff));
        buff[0] = 0;
        sleep(1);
        mem[0] = 0;
    }
    shmdt(mem);//去关联
    return 0;


}